package fm.dongman.animefm.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import fm.dongman.animefm.R;
import java.util.List;

import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.modellib.model.MsgModel;

/**
 * 聊天列表适配器
 * Created by Administrator on 2017/11/28.
 */

public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.Viewholder>{

    private Context mContext;
    private List<IModel.IMsgModel> mList;

    public ChatListAdapter(Context Context, List<IModel.IMsgModel> List){
        mContext=Context;
        mList=List;
    }

    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_chat_list,null);
        return new Viewholder(view);
    }

    @Override
    public void onBindViewHolder(Viewholder holder, int position) {

        final MsgModel msg= (MsgModel) mList.get(position);
        Glide.with(mContext).load(msg.getUserUrl()).into(holder.mIvChatUserhead);
        holder.mTvChatUserName.setText(msg.getTagName());
        holder.mTvChatListContext.setText(msg.getContent());
        holder.mTvChatUserTime.setText(msg.getCreateTime());
        //点击头像进入用户详情
        holder.mIvChatUserhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,SingleActivity.class);
                intent.putExtra(SingleActivity.Companion.getEXTRA_NAME(),SingleActivity.Companion.getINTENT_EXTRA_USER_INFO());
                intent.putExtra(SingleActivity.Companion.getBUNDLE_EXTRA_KEY_ID(),msg.getUserId());
                mContext.startActivity(intent);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,SingleActivity.class);
                intent.putExtra(SingleActivity.Companion.getEXTRA_NAME(),SingleActivity.Companion.getINTENT_EXTRA_CHAT());
                intent.putExtra(SingleActivity.Companion.getBUNDLE_EXTRA_KEY_TYPE(),msg.getUserId());
                String str=msg.getmTargetName();
                str=str.substring(str.indexOf('【')+1,str.indexOf('】'));
                intent.putExtra(SingleActivity.Companion.getBUNDLE_EXTRA_KEY_NAME(),str);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class Viewholder extends RecyclerView.ViewHolder{
        private ImageView mIvChatUserhead;
        private TextView mTvChatUserName;
        private TextView mTvChatListContext;
        private TextView mTvChatUserTime;

        public Viewholder(View itemView) {
            super(itemView);
            mIvChatUserhead= (ImageView) itemView.findViewById(R.id.mIvChatUserhead);
            mTvChatUserName= (TextView) itemView.findViewById(R.id.mTvChatUserName);
            mTvChatListContext= (TextView) itemView.findViewById(R.id.mTvChatListContext);
            mTvChatUserTime= (TextView) itemView.findViewById(R.id.mTvChatUserTime);



        }
    }
}
